﻿using Beway.ValueAdded.Model;
using Beway.ValueAdded.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Beway.ValueAdded.Service
{
    public class BatchRechargeSvc
    {
        public BatchRecharge Get(int id)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                var m = db.BatchRecharge.Find(id);
                return m;
            }
        }
        public List<BatchRechargeView> List(int page, int rows, int reUserId, int batchStatus, string batchName, DateTime? beginTime, DateTime? endTime, int uploadUserId, int adminId, out int ListCount)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                IQueryable<BatchRechargeView> q = from r in db.BatchRecharge
                                                  join p in db.ReUser on r.ReUserId equals p.UserID
                                                  join u in db.AdminUser on r.UploadUserId equals u.AdminID
                                                  join a in db.AdminUser on p.AdminId equals a.AdminID
                                                  join c in db.AdminUser on r.CheckUserId equals c.AdminID into checkTmp
                                                  from check in checkTmp.DefaultIfEmpty()
                                                  select new BatchRechargeView
                                                  {
                                                      Id = r.Id,
                                                      AddTime = DateTime.Now,
                                                      Amount = r.Amount,
                                                      BatchName = r.BatchName,
                                                      BatchStatus = r.BatchStatus,
                                                      CheckStatus = r.CheckStatus,
                                                      CheckUser = check.AdminUserName + "(" + check.TrueName + ")",
                                                      CheckUserId = r.CheckUserId,
                                                      FileName = r.FileName,
                                                      ProcessTime = r.ProcessTime,
                                                      ProjectName = p.UserTitle,
                                                      ReUserId =r.ReUserId,
                                                      Total = r.Total,
                                                      UploadUser = u.AdminUserName + "(" + u.TrueName + ")",
                                                      UploadUserId = r.UploadUserId,
                                                      ProjectAdminId = p.AdminId??0
                                                  };

           
                if (uploadUserId > 0)
                {
                    q = q.Where(m => m.UploadUserId == uploadUserId);
                }
                if (reUserId > 0)
                {
                    q = q.Where(m => m.ReUserId == reUserId);
                }
                if (adminId > 0)
                {
                    q = q.Where(m => m.ProjectAdminId == adminId);
                }
                if (batchStatus > -1)
                {
                    q = q.Where(m => m.BatchStatus == batchStatus);
                }
                if (!string.IsNullOrEmpty(batchName))
                {
                    q = q.Where(m => m.BatchName.Contains(batchName));
                }
                if (beginTime != null)
                {
                    q = q.Where(m => m.AddTime >= beginTime);
                }
                if (endTime != null)
                {
                    q = q.Where(m => m.AddTime <= endTime);
                }
                q = q.OrderByDescending(m => m.Id);

                ListCount = q.Count();

                var adm = q.Skip((page - 1) * rows).Take(rows).ToList();
                return adm;
            }
        }

        /// <summary>
        /// 更新批次信息，只更新了checkstatus,checkuserid,batchstatus,processtime
        /// </summary>
        /// <param name="br"></param>
        /// <returns></returns>
        public bool Update(BatchRecharge br)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                var m = db.BatchRecharge.Find(br.Id);
                if (m != null)
                {
                    m.CheckStatus = br.CheckStatus;
                    m.CheckUserId = br.CheckUserId;
                    m.BatchStatus = br.BatchStatus;
                    m.ProcessTime = br.ProcessTime;
                    return db.SaveChanges() > 0;
                }
            }
            return false;
        }
    }
}
